<?php
/**
 * controller class for rolepermission table (table which stores permission for specific roles) 
 * @package naanal
 */
class ClsRolepermission extends ClsNaanalController
{
	/**
         * request
         * @package naanal
         * @var string 
         */
        public $request=null;
        
	/**
         * construct
         * @package naanal
         * @param string $controlpanel
         */
	function __construct()
	{
            parent::__construct();
            $this->request=ClsNaanalRequest::getInstance()->getAll();
	}
	/**
         * get redirect
         * @package naanal
         * @return string
         */
	function getRedirect()
	{
		return $this->redirect;
	}
	/**
         * insert
         * @package naanal
         * @return string
         */
	function insert()
	{
		$this->mode="insert";
		if($this->request["roleidsubmit"]>0)
		{
			
		}
		else 
		{
			$sql="delete from rolepermission where `role_id`='{$this->request["role_id"]}'";
			$this->objPDO->setQuery($sql);
			foreach($this->request["permission"] as $permission_id=>$data)
			{
				$sql="insert into `rolepermission` (`permission_id`,`role_id`) values ('$permission_id','{$this->request["role_id"]}')";
	      		$this->objPDO->setQuery($sql);
			}
		}
		return $this->create();
	}
	/**
         * update
         * @package naanal
         */
	function update()
	{
		$this->mode="update";
		$sql="update `userrole` set role_id=:role_id,user_id=:user_id where `id`=:id";
		$obj=new ClsWUserrole();
		$obj->setArray($this->request);
	    $this->objPDO->setQuery($sql,$obj);
	    $this->redirect="{$this->urlModuleParam}=userrole&{$this->urlActionParam}=list";
	}
	/**
         * edit
         * @package naanal
         * @return array
         */
	function edit()
	{
		$this->mode="edit";
		$sql="select * from userrole where id='{$this->request["id"]}'";
		$this->objPDO->setQuery($sql);
		$obj=new ClsWUserrole();
		$obj=$this->objPDO->getObject("ClsWUserrole");
		return array("form"=>$obj);
	}
	/**
         * create
         * @package naanal
         * @return array
         */
	function create()
	{
		$this->mode="create";
		///retrieve role
		$roleid_selected=isset($this->request["role_id"])?$this->request["role_id"]:null;
		$sql="select role.* from role,user where role.user_id=user.id and user.client_id=".getUserObject()->get_client_id();
		$this->objPDO->setQuery($sql);
		$arrRole=array();
		$arr=$this->objPDO->getAllAssoc();
		if(empty($arr)) return array("role_id"=>$roleid_selected,"arrRole"=>array(),"arrTable"=>array(),"arrRowPermission"=>array(),"arrRowFeature"=>array());
		foreach($arr as $ind=>$row)
		{
			if(is_null($roleid_selected))$roleid_selected=$row["id"];
			$arrRole[$row["id"]]=$row["name"];
		}
		///retrieve all the features
		$sql="select * from feature where isadmin=0";
		$this->objPDO->setQuery($sql);
		$arrTmpRow=$this->objPDO->getAllAssoc();
		$arrRowFeature=array();
		foreach($arrTmpRow as $ind=>$row)
		{
			$arrRowFeature[$row["id"]]=$row["name"];
		}
		
		///retrieve all the permissiontype
		$sql="select * from permissiontype";
		$this->objPDO->setQuery($sql);
		$arrTmpRow=$this->objPDO->getAllAssoc();
		$arrRowPermission=array();
		foreach($arrTmpRow as $ind=>$row)
		{
			$arrRowPermission[$row["id"]]=$row["name"];
		}
		
		///retrieve the permission available for features. if permission is available checkbox will be shown. if permission is not available, empty cell will be shown
		$arrTable=null;
		foreach($arrRowFeature as $feature_id=>$feature_name)
		{
			foreach($arrRowPermission as $permissiontype_id=>$permission_name)
			{
				$sql="select * from permission where permissiontype_id=".$permissiontype_id." and feature_id=".$feature_id;
				$this->objPDO->setQuery($sql);
				$arr=$this->objPDO->getAllAssoc();
				if($arr) ///permission exist
				{
					$arrPermissionID=$this->getPermissionColumn($roleid_selected);
					///arr[feature_id][permissiontype_id]=permission_id. the permission exist for feature
					///if permission id exist in permission table, the check box will checked
					if(in_array($arr[0]["id"],$arrPermissionID))
						$arrTable[$feature_id][$permissiontype_id]=array("role_id"=>$roleid_selected,"permission_id"=>$arr[0]["id"],"selected"=>"checked");
					else 
						$arrTable[$feature_id][$permissiontype_id]=array("role_id"=>$roleid_selected,"permission_id"=>$arr[0]["id"],"selected"=>"");
				}	
				else ///permission not exist
				{
					///the permission not exist for feature
					$arrTable[$feature_id][$permissiontype_id]=false;
				}
			}
		}
		return array("role_id"=>$roleid_selected,"arrRole"=>$arrRole,"arrTable"=>$arrTable,"arrRowPermission"=>$arrRowPermission,"arrRowFeature"=>$arrRowFeature);
	}
	/**
         * get Permission column
         * @package naanal
         * @param integer $roleid
         * @param string $field
         * @return string
         */
	public function getPermissionColumn($roleid,$field="id")
	{
		$sql="select * from rolepermission where role_id=".$roleid;
		$this->objPDO->setQuery($sql);
		$arrCol=array();
                $objW=ClsWrapper::getInstance("rolepermission");
                $arrObj=$objW->getObjectListBySQL($sql);
		foreach($arrObj as $obj)
		{
                    $arrCol[]=$obj->get_permission_id();
		}
		return $arrCol;
	}
	/**
         * delete
         * @package naanal
         */
	function delete()
	{
		$this->mode="delete";
		$obj=new ClsWUserrole();
		$obj->set_id($this->request["id"]);
  		$sql="delete from `userrole` where `id`=:id";
  		$this->objPDO->setQuery($sql,$obj);
  		$this->redirect="{$this->urlModuleParam}=userrole&{$this->urlActionParam}=list";
	}
	/**
         * show list
         * @package naanal
         * @return array
         */
	function show_list()
	{
		$this->mode="list";
		$sql="select * from userrole,user where userrole.user_id=user.id and user.client_id=".getUserObject()->get_client_id();
		$this->objPDO->setQuery($sql);
		$arrObj=$this->objPDO->getAllObject("ClsWUserrole");
		return array("list"=>$arrObj);		
	}
	/**
         * render
         * @package naanal
         * @return string
         */
	function render()
	{     
		$mode=$this->mode;
		if($mode=="list")
			return $this->show_list();
		else
			return $this->$mode();
	}
}
?>